Valmone / Bip Chat [Ogame]

// ==UserScript==
// @name         Bip Chat [Ogame]
// @namespace    http://tampermonkey.net/
// @version      1.6
// @author       Valmone
// @include      *.ogame.gameforge.com/game*
// @updateURL    https://openuserjs.org/meta/Valmone/Bip_Chat_[Ogame].meta.js
// @copyright    2017, Valmone (https://openuserjs.org/users/Valmone)
// @license      MIT
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @require      http://code.jquery.com/jquery-3.5.0.min.js
// @require      https://raw.githubusercontent.com/Nickersoft/push.js/master/bin/push.js
// ==/UserScript==


window.onload = function() {

    function pushNotif(titre, msg) {
        if (Push.Permission.has())
            Push.create(titre, {
                body: msg,
                icon: "https://media.discordapp.net/attachments/779404100529684533/845643112884731924/happy.png",
                timeout: 15000,
                onClick: function () {
                    window.focus();
                    this.close();
                }
            });
        else
            Push.Permission.request(onGranted, onDenied);
    }

    const pays = document.getElementsByName("ogame-language")[0].getAttribute("content");
    const audio = new Audio("https://freesound.org/data/previews/342/342755_5260872-lq.mp3");

    if (pays.search("fr") >= 0) {
        var sound_on = '[Eteindre le Son]';
        var sound_off = '[Activer le Son]';
        var msg_notif = 'Vous avez un Nouveau Message';
    } else {
        var sound_on = '[Switch Off Sound]';
        var sound_off = '[Switch On Sound]';
        var msg_notif = 'You have a New Message';
    }

    var text = '',
        sp1 = document.createElement("span"); // on crée une balise span
    sp1.setAttribute("id", "Bip_Chat"); // on y ajoute un id
    var sp1_content = document.createTextNode('');
    sp1.appendChild(sp1_content);
    var sp2 = document.getElementsByClassName('OGameClock')[0], // Lieu où on veut afficher (A remplacer par ce que vous voulez
        parentDiv = sp2.parentNode;
    parentDiv.insertBefore(sp1, sp2.nextSibling);
    var tableau = document.createElement("span");
    tableau.innerText = text; // Ce qu'on veut afficher
    document.getElementById('Bip_Chat').insertBefore(tableau, document.getElementById('Bip_Chat').secondChild); // Affichage

    var etat = GM_getValue('etat');
    if (etat === 'on') {
        document.getElementById('Bip_Chat').innerText = sound_on;
        document.getElementById('Bip_Chat').style.color = "green";
    } else if (etat === 'off') {
        document.getElementById('Bip_Chat').innerText = sound_off;
        document.getElementById('Bip_Chat').style.color = "red";
    } else {
        document.getElementById('Bip_Chat').innerText = sound_on;
        document.getElementById('Bip_Chat').style.color = "green";
    }

    var survol = document.getElementById("Bip_Chat"),
        couleur = survol.style.color;


    var inter = setInterval(restart_script, 4000);
    function restart_script(){

        survol.addEventListener("mouseenter", function( event ) {
            event.target.style.color = "orange";
        });

        survol.addEventListener("mouseout", function( event ) {
            event.target.style.color = couleur;
        });

        if (document.getElementById('Bip_Chat').innerText === sound_on) {
            var mp = parseInt(document.querySelector(".totalChatMessages ").textContent);

            if (mp !== 0) {
                audio.play();
                pushNotif(msg_notif, $("[name='ogame-universe-name']").attr("content"));
                clearInterval(inter);
                inter = setInterval(restart_script, 60000);
            }

            document.getElementById('Bip_Chat').onclick = function() {
                document.getElementById('Bip_Chat').innerText = sound_off;
                document.getElementById('Bip_Chat').style.color = "red";
                GM_setValue('etat', 'off');
                couleur = 'red';
            };
        } else if (document.getElementById('Bip_Chat').innerText === sound_off) {
            document.getElementById('Bip_Chat').onclick = function() {
                document.getElementById('Bip_Chat').innerText = sound_on;
                document.getElementById('Bip_Chat').style.color = "green";
                GM_setValue('etat', 'on');
                couleur = 'green';
            };
        }
    }
};